package com.zmkj.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmkj.dao.SysSubmitWorkDao;
import com.zmkj.dao.SysWorkDao;
import com.zmkj.domain.SubmitData;
import com.zmkj.domain.SysSubmitWork;
import com.zmkj.domain.SysWord;
import com.zmkj.service.SysWorkService;
import com.zmkj.utils.Code;
import com.zmkj.utils.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;



@Service
public class SysWorkServiceImp implements SysWorkService {

    @Autowired
    private SysWorkDao sysWorkDao;
    @Autowired
    private SysSubmitWorkDao sysSubmitWorkDao;

    @Override
    public Rest AddWordData(SysWord sysWord) {
        sysWorkDao.insert(sysWord);
        return new Rest(Code.rc200.getCode(),"作业发布成功");
    }

    @Override
    public Rest getWordData(Integer userid) {
//        LambdaQueryWrapper<SysWord> sysWordLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        sysWordLambdaQueryWrapper.eq(SysWord::getUserId,userid);
//        List<SysWord> sysWordsdata = sysWorkDao.selectList(sysWordLambdaQueryWrapper);
//        return new Rest(Code.rc200.getCode(), sysWordsdata,Code.rc200.getMsg());


        LambdaQueryWrapper<SysSubmitWork> subwrapper = new LambdaQueryWrapper<>();
        subwrapper.eq(SysSubmitWork::getUserId,userid);
        List<SysSubmitWork> subdata = sysSubmitWorkDao.selectList(subwrapper);
        ArrayList<SubmitData> submitData = new ArrayList<>();
        for(SysSubmitWork d : subdata){
            LambdaQueryWrapper<SysWord> sysWordLambdaQueryWrapper = new LambdaQueryWrapper<>();
            SubmitData sd = new SubmitData();
            sysWordLambdaQueryWrapper.eq(SysWord::getId,d.getWorkId());
            SysWord workData = sysWorkDao.selectOne(sysWordLambdaQueryWrapper);
            sd.setWorkTitle(workData.getWorkTitle());
            sd.setSubmitNumber(d.getSubmitNum());
            if(workData.getPyMethod() == 2){
                sd.setPyMethod("学生自评");
            } else if (workData.getPyMethod() == 1) {
                sd.setPyMethod("师评");
            }else{
                sd.setPyMethod("评阅方式失败");
            }
            submitData.add(sd);
        }
        return new Rest(Code.rc200.getCode(), submitData,Code.rc200.getMsg());
    }
}
